package com.df.bpm.utils.businessViewerV2.business;


import com.df.bpm.utils.businessViewerV2.Main;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;

public class Db_business {

    String ctrllerPath="businessInformationV2/";


    public Integer deleteBusiness(Integer businessInformationIndex) {
        if(businessInformationIndex!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url= Main.server+ctrllerPath+"entirelyById?businessInformationIndex="+businessInformationIndex;
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.DELETE,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer updateBusiness(M_business temp) {
        if(temp.getBusinessDesc()!=null
                && !temp.getBusinessDesc().isEmpty()
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+ctrllerPath+"";
            HttpEntity<M_business> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    /**
     * 新增业务
     * @param temp  携带输出物的业务dto
     * @return  新增成功后的业务id
     */
    public Integer newBusiness(M_businessDto4CreateWithOutput temp) {
        if(temp!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+ctrllerPath+"";
            HttpEntity<M_businessDto4CreateWithOutput> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.POST,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public List<M_businessDto> getBusinessDto(Integer entityId4Output) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"businessInformationDTO/outputsEntityId?outputsEntityId="+entityId4Output;
        ResponseEntity<List<M_businessDto>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_businessDto>>() {}
        );
        return response.getBody();
    }

    public List<Integer> getGroupIds(Integer superior) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"groupId/superior?superior="+superior;
        ResponseEntity<List<Integer>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<Integer>>() {}
        );
        return response.getBody();
    }

    public List<M_businessLocationsDTO4Subset> getLocationDto4Subsets(Integer superior, Integer groupId) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"businessLocationsDTO4Subset/superiorAndGroupId?superior="
                +superior+"&groupId="+groupId;
        ResponseEntity<List<M_businessLocationsDTO4Subset>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_businessLocationsDTO4Subset>>() {}
        );
        return response.getBody();
    }

    public List<M_businessInputDto> getInputDtos(Integer business) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"businessInputDto/businessId?businessInformationIndex="
                +business;
        ResponseEntity<List<M_businessInputDto>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_businessInputDto>>() {}
        );
        return response.getBody();
    }

    public List<M_businessOutputDto> getOutputDtos(Integer business) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"businessOutputDto/businessId?businessInformationIndex="
                +business;
        ResponseEntity<List<M_businessOutputDto>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_businessOutputDto>>() {}
        );
        return response.getBody();
    }

    public List<M_businessLocationsDTO4Subset> getSuperiorDtos(Integer business) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"businessLocationsDTO4Subset?businessInformationIndex="
                +business;
        ResponseEntity<List<M_businessLocationsDTO4Subset>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_businessLocationsDTO4Subset>>() {}
        );
        return response.getBody();
    }

    public void freshInputOutput(Integer businessInformationIndex) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+ctrllerPath+"freshBusinessEntities?businessInformationIndex="
                +businessInformationIndex;
        restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<Objects>() {}
        );
    }

    public Integer updateOutput(M_businessOutput temp) {
        if(temp!=null){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+ctrllerPath+"businessOutputs";
            HttpEntity<M_businessOutput> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer updateInput(M_businessInput temp) {
        if(temp!=null){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+ctrllerPath+"businessInputs";
            HttpEntity<M_businessInput> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer deleteInput(Integer businessInputsIndex) {
        if(businessInputsIndex!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url= Main.server+ctrllerPath+"businessInputsIndex?businessInputsIndex="+businessInputsIndex;
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.DELETE,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer deleteOutput(Integer businessOutputsIndex) {
        if(businessOutputsIndex!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url= Main.server+ctrllerPath+"businessOutputsIndex?businessOutputsIndex="+businessOutputsIndex;
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.DELETE,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer deleteBusinessLocations(Integer businessLocationsIndex) {
        if(businessLocationsIndex!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url= Main.server+ctrllerPath+"businessLocationsIndex?businessLocationsIndex="+businessLocationsIndex;
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.DELETE,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer updateLocation(M_businessLocationsDTO4Subset temp) {
        if(temp!=null){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+ctrllerPath+"businessLocations";
            HttpEntity<M_businessLocationsDTO4Subset> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public List<M_businessDto4Search> getBusinessDto4Search(M_businessDto4Search temp) {
        RestTemplate restTemplate = new RestTemplate();
        String tempString=Main.printNonNullFields(temp);
        String url=Main.server+ctrllerPath+"businessInformationDTO4Search?"+tempString;
        ResponseEntity<List<M_businessDto4Search>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_businessDto4Search>>() {}
        );
        return response.getBody();
    }
}
